package datastructurs.linkedlist;

import java.util.Stack;

public class SingleLinkedList {
    public static void main(String[] args) {
        //创建节点
       HeroNode heroNode1 = new HeroNode(1,"A","a");
       HeroNode heroNode2 = new HeroNode(2,"B","b");
       HeroNode heroNode3 = new HeroNode(3,"C","c");
       HeroNode heroNode4 = new HeroNode(4,"D","d");
       HeroNode heroNode5 = new HeroNode(5,"E","e");
       HeroNode heroNode6 = new HeroNode(6,"F","f");
       HeroNode heroNode7 = new HeroNode(7,"G","g");
       HeroNode heroNode8 = new HeroNode(8,"H","h");
       HeroNode heroNode9 = new HeroNode(9,"I","i");
       HeroNode heroNode10= new HeroNode(10,"J","j");
       //初始化链表
        SingleList singleList = new SingleList();
//        singleList.addList(heroNode1);
//        singleList.addList(heroNode3);
//        singleList.addList(heroNode2);
//        singleList.addList(heroNode4);
        //按编号的顺序添加
        singleList.addByOrder(heroNode1);
        singleList.addByOrder(heroNode4);
        singleList.addByOrder(heroNode2);
        singleList.addByOrder(heroNode3);
        singleList.addByOrder(heroNode5);
        singleList.addByOrder(heroNode6);
        singleList.addByOrder(heroNode7);
        singleList.addByOrder(heroNode8);
        singleList.addByOrder(heroNode9);
        singleList.addByOrder(heroNode10);
//        System.out.println("Before modify:");
//        singleList.getList();
//        //修改节点
//        HeroNode newHeroNode = new HeroNode(2,"L","l");
//        singleList.update(newHeroNode);
//        System.out.println("After modify:");
//        //输出单链表
//        singleList.getList();
//        //删除节点
//        singleList.delete(5);
//        System.out.println("After Delete:");
        singleList.getList();
        System.out.println("List has nodes:"+getLength(singleList.getHead()));
        HeroNode res = findLastIndexNode(singleList.getHead(),2);
        System.out.println("倒数第2个："+res);
        //单链表的反转
        System.out.println("Invert a single linked list:");
        reverseList(singleList.getHead());
        singleList.getList();
        System.out.println("Reverse printing:");
        reversePrint(singleList.getHead());
    }
    //获取链表的有效节点
    public static int getLength(HeroNode head){
        if(head.next==null){
            return 0;
        }
        int length = 0;
        HeroNode cur = head.next;
        while (cur!=null){
            length++;
            cur = cur.next;
        }
        return length;
    }
    //查找链表中的倒数第k个节点(新浪面试题)：1.接受head节点，同时接受一个index(表示倒数第index个节点),2.遍历链表得到链表长度，3.得到size后从链表的第一个开始遍历（size-index)个,4找到返回该节点，否则返回null
    public static HeroNode findLastIndexNode(HeroNode head,int index){
        if(head.next==null){
            return null;//没有找到
        }
        //第一次遍历取得链表长度
        int size = getLength(head);
        //第二次遍历，size-index
        if(index<=0||index>size){
            return null;
        }
        //定义一个辅助变量,
        HeroNode cur = head.next;
        for(int i=0;i<size-index;i++){
            cur = cur.next;
        }
        return cur;
    }
    //单链表反转（腾讯面试题）
    public static void reverseList(HeroNode head){
        //若链表为空或者只有一个节点无需反转
        if(head.next==null||head.next.next==null){
            return;
        }
        //定义一个辅助指针，用于遍历原来的链表
        HeroNode cur = head.next;
        HeroNode next = null;//指向当前节点[cur]的下一个节点
        HeroNode reverseHead = new HeroNode(0,"","");
        //遍历原来的链表，每遍历一个节点就取出
        while (cur!=null){
            next = cur.next;//暂时保存当前节点的下一个节点，后面需要
            cur.next = reverseHead.next;//将cur的下一个节点指向新的链表的最前端
            //头结点指向取出的节点
            reverseHead.next = cur;
            cur = next;//让cur后移
        }
        //将头结点head.next指向reverse.next
        head.next = reverseHead.next;
    }
    //逆序输出:将链表放入栈zhong
    public static void reversePrint(HeroNode head){
        if(head.next==null){
            return;
        }
        //创建一个栈，将各个节点压入栈
        Stack<HeroNode> stack = new Stack<>();
        HeroNode cur = head.next;
        //将所有节点压入栈
        while(cur!=null){
            stack.push(cur);
            cur = cur.next;//后移
        }
        //将栈输出
        while (stack.size()>0){
            System.out.println(stack.pop());
        }
    }

}
class SingleList{
    //初始化一个头结点
    private HeroNode head = new HeroNode(0,"","");

    //返回头结点
    public HeroNode getHead() {
        return head;
    }

    //添加节点到单项链表:当不考虑编号的顺序时，找到当前链表的最后一个节点，最后节点next指向新的节点
    public void addList(HeroNode heroNode){
        //头节点不动，用一个辅助遍历temp
        HeroNode temp = head;
        //遍历链表到末尾
        while(true){
            if(temp.next==null){
                break;
            }
            //若未找到尾结点，将temp后羿
            temp = temp.next;
        }
        //指向新的节点
        temp.next = heroNode;
    }
    //输出链表
    public void getList(){
        //判断链表是否为空
        if(head.next==null){
            System.out.println("链表为空");
        }
        HeroNode temp = head.next;
        while(true){
            //判断尾结点
            if(temp==null){
                break;
            }
            //输出节点信息
            System.out.println(temp);
            //将temp的next后移
            temp = temp.next;
        }
    }
    //插入节点
    public void addByOrder(HeroNode heroNode){
        //通过辅助指针找到添加的位置(单链表）,位于添加位置的前一个节点
        HeroNode temp = head;
        boolean flag = false;//添加的编号是否存在，默认为false
        while (true){
            if (temp.next==null){//说明链表到末端
                break;
            }
            if (temp.next.no>heroNode.no){//位置找到，在temp后面插入
                break;
            }else if (temp.next.no==heroNode.no){
                //说明希望添加的heroNode的编号已经存在
                flag = true;
                break;
            }
            temp = temp.next;//后移
        }
        //判断flag
        if (flag){
            //不能添加，编号存在
            System.out.printf("The node number [%d] already exists.\n",heroNode.no);
        }else {
            //插入到链表中
            heroNode.next = temp.next;
            temp.next = heroNode;
        }

    }
    //根据编号修改节点
    public void update(HeroNode heroNode){
        //判断链表是否为空
        if(head.next==null){
            System.out.println("The list is empty!");
        }
        HeroNode temp = head.next;
        boolean flag = false;
        while(true){
            if(temp==null){
                break;//遍历完链表
            }
            if(temp.no==heroNode.no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag=true){
            temp.name = heroNode.name;
            temp.nickname = heroNode.nickname;
        }else {
            System.out.printf("No found [%d] node,can not change!\n",heroNode.no);
        }
    }
    //删除节点
    public void delete(int no){
        HeroNode temp = head;
        boolean flag = false;
        while(true){
            if(temp.next==null){
                break;
            }
            if(temp.next.no==no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag){
            temp.next = temp.next.next;
        }else {
            System.out.printf("No found [%d] node!.\n",no);
        }
    }
}
class HeroNode{
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;//指向下一个节点

    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
